package br.com.lazyprogrammer.spaceshooter.screen;

import android.util.Log;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import br.com.lazyprogrammer.spaceshooter.manager.ImageManager;
import br.com.lazyprogrammer.spaceshooter.model.World;
import br.com.lazyprogrammer.spaceshooter.model.listener.WorldListener;
import br.com.lazyprogrammer.spaceshooter.model.renderer.WorldRenderer;
import br.com.limaogames.framework.gl.Camera2D;
import br.com.limaogames.framework.gl.FPSCounter;
import br.com.limaogames.framework.gl.SpriteBatcher;
import br.com.limaogames.framework.impl.GLGame;
import br.com.limaogames.framework.impl.GLScreen;
import br.com.limaogames.framework.interfaces.Input;
import br.com.limaogames.framework.math.Vector2;
import br.com.limaogames.framework.util.Display;

/**
 * Created by LimaoGames on 19/8/2014.
 */
public class MainScreen extends GLScreen {
    Camera2D guiCam;
    SpriteBatcher batcher;
    Vector2 touchPoint;

    World world;
    WorldRenderer wRenderer;

    FPSCounter fpsCounter;

    public MainScreen(GLGame game) {
        super(game);

        guiCam = new Camera2D(glGraphics, WorldRenderer.FRUSTUM_WIDTH, WorldRenderer.FRUSTUM_HEIGHT);
        batcher = new SpriteBatcher(glGraphics, 100);
        touchPoint = new Vector2();

        world = new World();
        wRenderer = new WorldRenderer(glGraphics, batcher, world);

        fpsCounter = new FPSCounter();
    }

    @Override
    public void update(float deltaTime) {
        if (deltaTime > 0.1f)
            deltaTime = 0.1f;
        switch (world.state) {
            case World.WORLD_STATE_GAME_OVER:
                break;

            case World.WORLD_STATE_PAUSED:
                break;

            case World.WORLD_STATE_RUNNING:
                updateRunning(deltaTime);
                break;

            case World.WORLD_STATE_START:
                break;
        }
        fpsCounter.logFrame();
    }

    public void updateRunning(float deltaTime) {
        world.update(deltaTime, game.getInput().getAccelX());
        List<Input.TouchEvent> touchEvents = game.getInput().getTouchEvents();
        int len = touchEvents.size();
        for (int i = 0; i < len; i++) {
            Input.TouchEvent touchEvent = touchEvents.get(i);
            if (touchEvent.type == Input.TouchEvent.TOUCH_UP) {
                world.spaceShoot();
                break;
            }
        }
    }

    @Override
    public void present(float deltaTime) {
        GL10 gl = glGraphics.getGL();
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        gl.glEnable(GL10.GL_TEXTURE_2D);

        switch (world.state) {
            case World.WORLD_STATE_GAME_OVER:
                break;

            case World.WORLD_STATE_START:
                break;

            case World.WORLD_STATE_RUNNING:
                presentRunning();
                break;

            case World.WORLD_STATE_PAUSED:
                break;
        }

        gl.glDisable(GL10.GL_BLEND);
    }

    public void presentRunning() {
        wRenderer.render();
    }

    @Override
    public void pause() {
        world.state = World.WORLD_STATE_PAUSED;
    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {

    }
}
